package com.lovo.test.ATM;

import com.lovo.test.mysql.Student;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class AtmMysq {
    public static Connection getConnection() {
        Connection con = null;
        try {
            // 1. 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2. 创建连接
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/ATM?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                    "root", "Xiaohao123456");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }

        return con;
    }

    //增删改连接关闭
    public static void close(Connection con, Statement statement) {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }

    public static void close(Connection con, Statement statement, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }

    /**
     *
     * @param user 传入要修改数据的对象
     */
    public static void modify1(User user ) {
        //创建连接
        Connection con = getConnection();
        PreparedStatement statement = null;
        String sql = "update t_user set u_moneys=?,u_pwd=? where u_id=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setInt(1,user.getMoney());
            statement.setString(2, user.getPassWord());
            statement.setInt(3,user.getId());
            statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement);
        }
    }

    public static void turnModify(User myUser,User objUser ) {
        //创建连接
        Connection con = getConnection();
        PreparedStatement statement = null;
        String sql = "update t_user set u_moneys=?,u_pwd=? where u_id=?";
        try {
            con.setAutoCommit(false);
            statement = con.prepareStatement(sql);
            statement.setInt(1,myUser.getMoney());
            statement.setString(2, myUser.getPassWord());
            statement.setInt(3,myUser.getId());
            statement.executeUpdate();


            statement = con.prepareStatement(sql);
            statement.setInt(1,objUser.getMoney());
            statement.setString(2, objUser.getPassWord());
            statement.setInt(3,objUser.getId());
            statement.executeUpdate();
            con.commit();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } finally {
            close(con, statement);
        }
    }


    /**
     * 根据用户名和密码查询用户是否存在，实现登陆
     * @param name 用户名
     * @param pwd 密码
     * @return
     */
    public  static User loginCX(String name,String pwd){
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        User user =null;
        String sql="select * from t_user where u_name=? and u_pwd=?";
        try {
            statement =  con.prepareStatement(sql);
            statement.setString(1,name);
            statement.setString(2,pwd);
            rs=statement.executeQuery();
            if (rs.next()){
                user = new User();
                user.setId(rs.getInt("u_id"));
                user.setUserName(rs.getString("u_name"));
                user.setPassWord(rs.getString("u_pwd"));
                user.setMoney(rs.getInt("u_moneys"));
//
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(con,statement,rs);
        }
        return user;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public  static  User getById(int id){
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        User user = null;
        String sql = "select * from t_user where u_id=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setInt(1,id);
            rs = statement.executeQuery();
            if(rs.next()){
                user = new User();
                user.setId(rs.getInt("u_id"));
                user.setUserName(rs.getString("u_name"));
                user.setPassWord(rs.getString("u_pwd"));
                user.setMoney(rs.getInt("u_moneys"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con,statement,rs);
        }
        return user;
    }

    /**
     * 根据用户名查询
     * @param name
     * @return
     */
    public  static  User getByName(String name){
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        User user = null;
        String sql = "select * from t_user where u_name=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setString(1,name);
            rs = statement.executeQuery();
            if(rs.next()){
                user = new User();
                user.setId(rs.getInt("u_id"));
                user.setUserName(rs.getString("u_name"));
                user.setPassWord(rs.getString("u_pwd"));
                user.setMoney(rs.getInt("u_moneys"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con,statement,rs);
        }
        return user;
    }




    //查询
    public static ArrayList query() {
        //创建连接
        Connection con = getConnection();
        Statement statement = null;
        ResultSet rs = null;
        ArrayList<User> userList = new ArrayList();
        try {
            statement = con.createStatement();
            String sql = "select * from t_user";
            rs = statement.executeQuery(sql);
            while (rs.next()) {
                User users = new User();
                users.setId(rs.getInt("u_id"));
                users.setUserName(rs.getString("u_name"));
                users.setPassWord(rs.getString("u_pwd"));
                users.setMoney(rs.getInt("u_moneys"));
                userList.add(users);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return userList;
    }
}
